package com.wish.group05.shoufang.sign;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.alibaba.nacos.common.utils.UuidUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itextpdf.io.codec.Base64;
import com.itextpdf.kernel.geom.Rectangle;
import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
import com.wish.group05.base.entity.*;
import com.wish.group05.base.service.*;
import com.wish.group05.common.core.utils.IdUtils;
import com.wish.group05.common.core.utils.StringUtils;
import com.wish.group05.shoufang.sign.*;
import com.wish.group05.shoufang.util.PDFUtils;
import org.bouncycastle.util.io.Streams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.wish.group05.common.log.annotation.Log;
import com.wish.group05.common.log.enums.BusinessType;
import com.wish.group05.common.security.annotation.PreAuthorize;

import com.wish.group05.common.core.web.controller.BaseController;
import com.wish.group05.common.core.web.domain.AjaxResult;
import com.wish.group05.common.core.utils.poi.ExcelUtil;
import com.wish.group05.common.core.web.page.TableDataInfo;

@Service
public class MarkService {

    public void markPdf() {
        DeferredSignPdfAssLry signPdfAss = new DeferredSignPdfAssLry();
        Rectangle rectangle = new Rectangle(36, 448, 200, 100);
        SignInfo signInfo = new SignInfo(rectangle,1,"第一次签名","维数公寓","SIGNATURE_A",DeferredSignPdfAssLryTest.singImg,true);
        //获取空的签名文件
        ExternalEmptySignatureContainer externalSignatureContainer = new ExternalEmptySignatureContainer(signPdfAss.getDicFilter(),signPdfAss.getDicSubfilter(),signPdfAss.getHashAlgorithm());
        //初始化
        try {
            signPdfAss.initSigner(Streams.readAll(new FileInputStream(DeferredSignPdfAssLryTest.SOURCEFILE)),DeferredSignPdfAssLryTest.TEMPFILE,signInfo,externalSignatureContainer);
        } catch (IOException | GeneralSecurityException e) {
            System.out.println("初始化签名对象异常");
            e.printStackTrace();
        }
        //待签名哈希,发给外部服务器进行签名
        String ToSignHash = Base64.encodeBytes(externalSignatureContainer.getDigestHash()) ;
        String signedHash = null;//cfca签名后的哈希值
        try {
            signedHash = SignService.sign(ToSignHash,signPdfAss.getHashAlgorithm());
        } catch (GeneralSecurityException e) {
            System.out.println("签名异常！");
            e.printStackTrace();
        } catch (IOException e) {
            System.out.println("签名异常！");
            e.printStackTrace();
        }
        //验签，这一步需要通过外部服务器返回的signedHash，与原文件哈希值externalSignatureContainer.getDigestHash()进行验证
        byte[] cmsInfo = Base64.decode(signedHash);
        try {
            DeferredSignPdfAssLry.embedSignature(DeferredSignPdfAssLryTest.TEMPFILE,DeferredSignPdfAssLryTest.RESULTFILE,"SIGNATURE_A",new EmbdedSignatureContainer(cmsInfo));
        } catch (IOException e) {
            System.out.println("分离签名异常！");
            e.printStackTrace();
        } catch (GeneralSecurityException e) {
            System.out.println("分离签名异常！");
            e.printStackTrace();
        }
    }

}
